function debounce(delay, callback) {
  let timer
  return function () {
    clearTimeout(timer)
    timer = setTimeout(() => {
      callback.apply(this, arguments)
    }, delay)
  }
}

jQuery(function ($) {
  $(document).on('click', '.toolbar a[data-target]', function (e) {
    e.preventDefault()
    var target = $(this).data('target')
    $('.widget-box.visible').removeClass('visible') //hide others
    $(target).addClass('visible') //show target
  })
})
//you don't need this, just used for changing background
$('#btn-login-dark').on('click', function (e) {
  $('body').attr('class', 'login-layout')
  $('#id-text2').attr('class', 'white')
  $('#id-company-text').attr('class', 'blue')

  e.preventDefault()
})
$('#btn-login-light').on('click', function (e) {
  $('body').attr('class', 'login-layout light-login')
  $('#id-text2').attr('class', 'grey')
  $('#id-company-text').attr('class', 'blue')

  e.preventDefault()
})
$('#btn-login-blur').on('click', function (e) {
  $('body').attr('class', 'login-layout blur-login')
  $('#id-text2').attr('class', 'white')
  $('#id-company-text').attr('class', 'light-blue')

  e.preventDefault()
})

function putData() {
  var name = document.getElementById('login_name').value
  var password = document.getElementById('login_password').value
//   if (name.length >= 16) {
//     alert('登录名过长，请保证16位以下')
//   } else if (password.length >= 16) {
//     alert('密码过长，请保证16位以下')
//   }
  let Options = {
    async: true,
    type: 'POST',
    url: './project/login',
    data: {
      login_name: name,
      login_password: password,
    },
    success: function (data) {
      data = JSON.parse(data)
      console.log(data)
      if (data.login_msg === 200) {
        sessionStorage.setItem('user_name', data.user_name)
        sessionStorage.setItem('user_role', data.user_role)
        sessionStorage.setItem('user_id', data.user_id)
        sessionStorage.setItem('user_login_name', data.user_login_name)
        sessionStorage.setItem('hasLeader', data.hasLeader)
        sessionStorage.setItem('login_msg', '登陆成功')
        window.location.href = './keshihua/index.html'
      } else {
        alert(data.login_msg)
      }
    },
    error: function () {
      alert('服务器开小差啦')
    },
  }

  $.ajax(Options)
}

document
  .querySelector('#login_btn')
  .addEventListener('click', debounce(1000, putData))

document.getElementById('group_leader').addEventListener('click', function () {
  document.getElementById('group_member').checked = false
})
document.getElementById('group_member').addEventListener('click', function () {
  document.getElementById('group_leader').checked = false
})

const register = function () {
  if (!$('#agree_or_not').is(':checked')) {
    alert('同意”相关服务条款和隐私政策“')
    return
  }

  var role = $('input.group_role:checked').val()
  var email = $('#email').val()
  var mobile = $('#mobile').val()
  var realName = $('#realName').val()
  var userName = $('#userName').val()
  var password = $('#password').val()
  var r_password = $('#r_password').val()
  if (role === undefined) {
    alert('请选择你的角色!')
    return
  }
  if (
    email === '' ||
    mobile === '' ||
    realName === '' ||
    userName === '' ||
    password === '' ||
    r_password === ''
  ) {
    alert('请完善基本信息!')
    return
  }
  if (password !== r_password) {
    alert('两次密码输入不一致！')
    return
  }
  if (!/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/.test(email)) {
    alert('邮箱不合法，请输入正确的邮箱号')
    return
  } else if (!/^(13|14|15|17|18|19)[0-9]{9}/.test(mobile)) {
    alert('手机号不合法，请输入正确的手机号')
    return
  } else if (userName.length >= 16) {
    alert('登录名过长，请保证在16位以内')
    return
  } else if (password.length >= 16) {
    alert('密码过长，请保证在16位以内')
    return
  }
  var data = {}
  data.role = role
  data.email = email
  data.mobile = mobile
  data.realName = realName
  data.userName = userName
  data.password = password

  $.post('./project/register', data, function (json) {
    json = JSON.parse(json)
    if (json.register_result === 1) {
      alert('注册成功！请返回登录')
      $('#signup-box').removeClass('visible')
      $('#login-box').addClass('visible')
    }
    if (json.register_result === 0) {
      alert('注册失败！' + json.register_fail_reason)
    }
  })
}


let emailLine = document.getElementById('email_line');
let codeLine = document.getElementById('code_line');
let getCodeBtn = document.getElementById('getCodeBtn');
let rGetCodeBtn = document.getElementById('rGetCodeBtn');
let sendCodeBtn = document.getElementById('sendCodeBtn');

codeLine.style.display = 'none';
sendCodeBtn.style.display = 'none';
rGetCodeBtn.style.display = 'none';

let verificationCode = "******";
let username = "******";
let password = "******";

const getCode = function () {
  let email = $('#email_address').val();
  if (!/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/.test(email)) {
    alert('邮箱不合法，请输入正确的邮箱号')
    return
  }
  var data = {}
  data.email = email
  alert("请求已发出，请耐心等待。")
  $.post('./project/getCode', data, function (json) {
    json = JSON.parse(json)
    if (json.result === 1) {
      alert("验证码已发送到您的邮箱，请查收！");
      verificationCode = json.code;
      username = json.username;
      password = json.password;
      emailLine.style.display = 'none';
      getCodeBtn.style.display = 'none';
      rGetCodeBtn.style.display = 'block';
      codeLine.style.display = 'block';
      sendCodeBtn.style.display = 'block';
    }
    if (json.result === 0) {
      alert(json.reason)
    }
  })
}

const sendCode=function (){
  let code = $('#code').val();
  if(code !== verificationCode){
    alert("验证码错误！");
    return;
  }
  alert("验证成功！");
  alert("您的登录名："+username+"\n您的登陆密码："+password);
}